/*
 *  Class name: Controls.java
 *  Version: 1.0
 *  Date: 3.5.2014
 */
package cz.muni.fi.pv112.cranesimulator.settings;

import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * Singleton class Controls stores the crane application controls settings
 *
 * @author Michal Ďuriš
 */
public class Controls {

    public enum Key {

        MOVE_CAMERA_FORWARD(0),
        MOVE_CAMERA_BACKWARD(1),
        MOVE_CAMERA_LEFT(2),
        MOVE_CAMERA_RIGHT(3),

        FREE_CAMERA_MODE(4),
        CABIN_CAMERA_MODE(5),
        MAGNET_CAMERA_MODE(6),

        MAGNET_UP(7),
        MAGNET_DOWN(8),
        MAGNET_ENABLED(9),

        RIDER_FORWARD(10),
        RIDER_BACKWARD(11),

        OPERATION_PART_ROTATION_LEFT(12),
        OPERATION_PART_ROTATION_RIGHT(13),

        UNDEFINED(99);

        private final int code;

        private Key(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }

    }

    private final Map<Key, Integer> controlsKeyMap = new HashMap<>(14);

    private Controls() {

        controlsKeyMap.put(Key.MOVE_CAMERA_FORWARD, KeyEvent.VK_UP);
        controlsKeyMap.put(Key.MOVE_CAMERA_BACKWARD, KeyEvent.VK_DOWN);
        controlsKeyMap.put(Key.MOVE_CAMERA_LEFT, KeyEvent.VK_LEFT);
        controlsKeyMap.put(Key.MOVE_CAMERA_RIGHT, KeyEvent.VK_RIGHT);

        controlsKeyMap.put(Key.FREE_CAMERA_MODE, KeyEvent.VK_1);
        controlsKeyMap.put(Key.CABIN_CAMERA_MODE, KeyEvent.VK_2);
        controlsKeyMap.put(Key.MAGNET_CAMERA_MODE, KeyEvent.VK_3);

        controlsKeyMap.put(Key.MAGNET_UP, KeyEvent.VK_E);
        controlsKeyMap.put(Key.MAGNET_DOWN, KeyEvent.VK_C);
        controlsKeyMap.put(Key.MAGNET_ENABLED, KeyEvent.VK_S);

        controlsKeyMap.put(Key.RIDER_FORWARD, KeyEvent.VK_W);
        controlsKeyMap.put(Key.RIDER_BACKWARD, KeyEvent.VK_X);

        controlsKeyMap.put(Key.OPERATION_PART_ROTATION_LEFT, KeyEvent.VK_A);
        controlsKeyMap.put(Key.OPERATION_PART_ROTATION_RIGHT, KeyEvent.VK_D);
    }

    public static Controls getInstance() {
        return ControlsHolder.INSTANCE;
    }

    private static class ControlsHolder {

        private static final Controls INSTANCE = new Controls();
    }

    public void setControlsKeyMapping(Key controlKey, int keyCode) {
        controlsKeyMap.put(controlKey, keyCode);
    }

    public int getControlsKeyMapping(Key controlKey) {
        return controlsKeyMap.get(controlKey);
    }

    public Key getControlsKey(int keyCode) {

        return controlsKeyMap.entrySet()
                .stream()
                .filter(entry -> entry.getValue() == keyCode)
                .map(entry -> entry.getKey())
                .findFirst().orElse(Key.UNDEFINED);
    }

}
